#include"Search.hpp"
#include"HpcServer/Server/HttpServer.hpp"

const string Basedir="./wwwroot";
const string ParserFilePath="./data/original/original.txt";

Search sea;

bool Searcher(HttpRequest &req, HttpResponse *res)
{
    //从查询字符串中拿出用户搜索的内容
    string keywords=req.GetParam("keyword");
    if(keywords=="")
    {
        //设置响应正文
        res->SetBody("搜索内容不能为空！","text/plain; charset=utf-8");
        return true;
    }

    LOG(LogLevel::DEBUG)<<"用户搜索的内容: "<<keywords;

    //查询
    string search_result;
    sea.SearchIndex(keywords,&search_result);

    //构建响应
    res->SetBody(search_result,"application/json");

    return true;
}

int main(int argc,char* argv[])
{
    if(argc!=2)
    {
        cout<<"server serverport"<<endl;
        return 1;
    }
    //设置日志只关心
    //SetValueLogLevel(LogLevel::DEADLY);

    //初始化搜索引擎
    sea.Init(ParserFilePath);

    //设置端口号，web根目录，连接超时销毁时间
    HttpServer server(stoi(argv[1]),Basedir,30);

    //设置从属Reactor线程个数
    server.SetReactorThreadCount(4);

    //把搜索函数注册到，GET方法的动态路由表中
    server.InsertGetHandles("/search",Searcher);
    
    //启动服务器
    server.Start();
    return 0;
}

